package com.kwedding.service.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kwedding.bean.TB_Brand;
import com.kwedding.bean.TB_BrandSorts;
import com.kwedding.dao.TB_BrandMapper;
import com.kwedding.dao.TB_BrandSortsMapper;
import com.kwedding.dao.TB_ImagesMapper;
import com.kwedding.dto.app.RequestBrandList;
import com.kwedding.dto.app.ResponseBrandList;
import com.kwedding.dto.app.ResponseBrandSortsList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.LoggerParent;

@Service
public class BrandService extends LoggerParent {

	@Autowired
	private TB_BrandSortsMapper tb_BrandSortsMapper;
	@Autowired
	private TB_BrandMapper tb_BrandMapper;
	@Autowired
	private TB_ImagesMapper tb_ImagesMapper;

	/**
	 * 获取品牌分类列表
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseBrandSortsList> getBrandSortsList() {
		List<TB_BrandSorts> brandSorts = tb_BrandSortsMapper.getBrandSortsList(false, null);
		List<ResponseBrandSortsList> responseBrandSortsLists = new ArrayList<>();
		if (brandSorts != null && brandSorts.size() > 0) {
			brandSorts.stream().forEach(r -> {
				ResponseBrandSortsList responseBrandSortsList = CopyerObject.copyerObject(r,
						ResponseBrandSortsList.class);
				responseBrandSortsLists.add(responseBrandSortsList);
			});
		}
		return responseBrandSortsLists;
	}

	/**
	 * 根据类型和城市获取品牌列表
	 * 
	 * @param brandList
	 */
	@Transactional(readOnly = true)
	public List<ResponseBrandList> getBrandList(RequestBrandList brandList) {
		CheckedUtil.isNull(brandList.getCityId(), Constants.REQUEST_ERROR_1, "城市代码不能为空");
		CheckedUtil.isNull(brandList.getSortsId(), Constants.REQUEST_ERROR_2, "分类不能为空");
		List<TB_Brand> tb_Brands = tb_BrandMapper.selectBrandListByCityAndSorts(brandList.getCityId(),
				brandList.getSortsId());
		List<ResponseBrandList> responseBrandLists = new ArrayList<>();
		if (tb_Brands != null && tb_Brands.size() > 0) {
			tb_Brands.stream().forEach(r -> {
				ResponseBrandList responseBrandList = CopyerObject.copyerObject(r, ResponseBrandList.class);
				// 查询品牌所有的类型
				List<Integer> types = tb_ImagesMapper.selectTypeByBrandId(r.getBrandId());
				responseBrandList.setTypes(types);
				responseBrandLists.add(responseBrandList);
			});
		}
		return responseBrandLists;
	}
}
